package com.scu.phototab.controller;

import com.scu.phototab.push.Http2LogicConnection;
import com.scu.phototab.util.ResponseUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import protobuf.ParseRegistryMap;
import protobuf.Utils;
import protobuf.generate.internal.Internal;
import protobuf.generate.srv2cli.push.Push;

import static com.scu.phototab.util.Predictions.checkNotNull;

/**
 * Created by tianfei on 2017/4/16.
 *
 * 通过获取到的PushServer实例向指定的在线用户推送消息；
 * 管理员API
 * （可做JSP页面实现在线管理员推送）
 * 不在线的用户暂时未实现       （后期再做消息队列   RabbitMQ）
 *
 */
@RestController
@RequestMapping(value = "push")
public class PushController {

    @PostMapping(value = "all")
    public Object pushAllUser(@RequestParam String title, @RequestParam String content){

        String _title=checkNotNull(title);
        String _content = checkNotNull(content);

        Push.SPushMsg msg=Push.SPushMsg.newBuilder()
                .setTitle(_title)
                .setContent(_content)
                .build();

        Http2LogicConnection.getHttp2LogicConnection().writeAndFlush(
                Utils.pack2Server(msg,ParseRegistryMap.SPUSH, Internal.Dest.Logic));

        return ResponseUtil.success();
    }

    @PostMapping(value = "tag")
    public Object pushWithTag(){

        return ResponseUtil.success();
    }




}
